﻿using System;
using System.Globalization;
using System.Threading;
using Commons.Collections;
using NetDon.OA.Business;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Interface;
using NetDon.OA.Interface.Configuration;
using NetDon.OA.Portal.Common;
using NetDon.Utilities;
using NVelocity.App;

namespace NetDon.OA.Portal
{
    public class Global : Microsoft.Practices.CompositeWeb.WebClientApplication
    {
        protected override void Application_Start(object sender, EventArgs e)
        {
            ServiceContainer.AddService<ApplicationContext>(Memoizer.CreateProxy<OApplicationContext>());
            ServiceBuilder.AddServices();

            ExtendedProperties properties = new ExtendedProperties();
            properties.AddProperty("file.resource.loader.path", Server.MapPath("~/Templates/"));
            Velocity.Init(properties);

            base.Application_Start(sender, e);
        }

        protected override void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            try
            {
                string lan = Request["language"];
                if (string.IsNullOrEmpty(lan))
                {
                    lan = SettingsManager.Globalization.DefaultCulture;
                }
                CultureInfo culture = CultureInfo.CreateSpecificCulture(lan);
                Thread.CurrentThread.CurrentUICulture = culture;
                Thread.CurrentThread.CurrentCulture = culture;
                base.Application_PreRequestHandlerExecute(sender, e);
            }
            catch (Exception)
            {
               
            }
          
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            var context = ServiceContainer.GetService<ApplicationContext>();
            context.ApplicationId = SystemConfiguration.OApplicationId;
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError().GetBaseException();
            var context = ServiceContainer.GetService<ApplicationContext>();

            if (ex != null && !ex.Message.Contains("File does not exist"))
            {

                var service = ServiceContainer.GetService<ILogService>();
                service.Log(new LoggingEvent
                {
                    //ApplicationId = context.ApplicationId,
                    Category = EventCategories.Security,
                    Level = LogLevel.Error,
                    Source = "应用程序",

                    Message =
                        ex.Message
                });

            }
        }

        protected void Session_End(object sender, EventArgs e)
        {
            var context = ServiceContainer.GetService<ApplicationContext>();
            var userId = context.GetUserId(this);
            if (userId != Guid.Empty)
            {
                var service = ServiceContainer.GetService<ILogService>();
                service.Log(new LoggingEvent
                {
                    ApplicationId = context.ApplicationId,
                    Category = EventCategories.Security,
                    Level = LogLevel.Logout,
                    Source = "安全",
                    UserName = context.UserName,
                    Message =
                        "Logout account:" + new UserEntity(userId).UserName + Environment.NewLine +
                        "Logout Application:" +
                        SystemConfiguration.ApplicationName
                });
            }
        }

        protected void Application_End(object sender, EventArgs e)
        {
            ServiceBuilder.RemoveServices();
        }
    }
}